﻿using System;
using System.Net;
using System.IO;
using System.Threading;

namespace AuctionHouseDataLibrary
{
    public class WebClientCustom
    {
        public static int Timeout = 10000;

        public static void DownloadFile(string url, string fileLocation)
        {
            HttpWebResponse response = null;
            Stream s = null;
            FileStream fs = null;
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = Timeout;
                request.AllowWriteStreamBuffering = false;
                AddHeaders(request);
                response = (HttpWebResponse)request.GetResponse();
                s = response.GetResponseStream();

                //Write to disk
                fs = new FileStream(fileLocation, FileMode.Create);
                var read = new byte[256];
                int count = s.Read(read, 0, read.Length);
                while (count > 0)
                {
                    fs.Write(read, 0, count);
                    count = s.Read(read, 0, read.Length);
                }

                //Close everything
                fs.Close();
                s.Close();
                response.Close();
            }
            finally
            {
                if (response != null)
                    response.Close();
                if (fs != null)
                    fs.Close();
                if (s != null)
                    s.Close();
            }
        }

        public static string DownloadString(string url)
        {
            HttpWebResponse response = null;
            StreamReader loResponseStream = null;
            string returnMe = "";
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = Timeout;
                request.AllowWriteStreamBuffering = false;
                AddHeaders(request);
                response = (HttpWebResponse)request.GetResponse();
                loResponseStream = new
                     StreamReader(response.GetResponseStream());
                returnMe = loResponseStream.ReadToEnd();
                loResponseStream.Close();
                response.Close();
            }
            catch (IOException)
            {
                return "";
            }
            finally
            {
                if (loResponseStream != null)
                    loResponseStream.Close();
                if (response != null)
                    response.Close();
            }
            return returnMe;
        }

        public static ReaderWriterLockSlim IncrementCount = new ReaderWriterLockSlim();
        public static int Counter;

        public static void AddHeaders(HttpWebRequest request)
        {
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(new Uri("http://4chan.org"), new Cookie("nws_style", "Futaba"));
        }
    }
}
